package com.xingkeduo.business.user.controller;

import com.xingkeduo.business.transformation.json.*;
import com.xingkeduo.business.user.domain.ManageInfoDomain;
import com.xingkeduo.pojo.dto.CraftsmanShopDTO;
import com.xingkeduo.pojo.query.CraftsmanQO;
import com.xingkeduo.pojo.query.CraftsmanShopQO;
import com.xingkeduo.pojo.table.CraftsmanShopTO;
import com.xingkeduo.pojo.utils.ResponseUtils;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.tool.ReturnUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 一线员工绑定店铺
 *
 * @author 高杰
 * @time 2019年1月12日 下午6:38:17
 * @description
 */
//@CrossOrigin
@RestController
@RequestMapping("/craftsman/shop")
public class CraftsmanShopController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(CraftsmanShopController.class);

    @Autowired
    private ManageInfoDomain manageInfoDomain;


    /**
     * 绑定店铺接口
     *
     * @param url
     * @param body
     * @return
     * @author 高杰
     * @time 2019年1月12日 下午6:38:02
     */
    @PostMapping(value = "/{url:.+}")
    public String doPut(@PathVariable String url, @RequestBody String body, ServerHttpRequest request) {
        log.info("url:" + url);
        log.info("body:" + body);
        returnUtil = new ReturnUtil();
        try {
            CraftsmanShopTO craftsmanShop = CraftsmanShopJTO.createTableForNew(body);
            log.info("craftsmanShop:" + craftsmanShop.toString());
            manageInfoDomain.modifyCraftsmanShop(craftsmanShop, getOperator(request));
            return returnUtil.getSuccess();
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 查店铺是否有发型师
     *
     * @return
     * @author 高杰
     * @time 2019年1月12日 下午6:38:02
     */
    @GetMapping(value = "/check/craftsman/{shopid}")
    public String doPut(@PathVariable String shopid) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanShopQO craftsmanShop = CraftsmanShopJTO.createQueryForShop(shopid);
            return returnUtil.getSuccess(BaseDTOJTO.toJson(manageInfoDomain.checkShopAndCraftsman(craftsmanShop)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 查询店铺员工
     *
     * @param craftsmanid: 当前员工id
     * @param shopId:      商铺id
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/30 13:56
     */
    @GetMapping("/{craftsmanid}")
    public String getCratfsmanShop(@PathVariable String craftsmanid, String shopId) {
        returnUtil = new ReturnUtil();
        try {
            CraftsmanShopQO craftsmanShop = CraftsmanShopJTO.createQueryForList(craftsmanid);
            craftsmanShop.setShopId(CraftsmanShopJTO.createQueryForShop(shopId).getShopId());
            return returnUtil.getSuccess(CraftsmanSocialJTO.toJsonForShop(manageInfoDomain.getCraftsmanShop(craftsmanShop)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 外部接口,查询店铺员工id及其个人信息
     *
     * @param staffIds:发型师ids
     * @param type:           用户头像
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/30 13:56
     */
    @PostMapping("/getshop")
    public String getShopMan(@RequestBody List<Long> staffIds, String type) {
        returnUtil = new ReturnUtil();
        try {
            return returnUtil.getSuccess(CraftsmanFileDTOJTO.toJson(manageInfoDomain.getShopCraftsmanInfo(staffIds, type)));
        } catch (Throwable t) {
            return dealException(t, returnUtil);
        }
    }


    /**
     * 门店列表 (经理身份)  经理可管理多个门店，门店只有一位经理
     *
     * @param id:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/28 10:23
     */
    @PostMapping("/userlist/{id:.+}")
    public String getCraftsmanShop(@PathVariable String id) {
        returnUtil = new ReturnUtil();
        try {
            log.info("shop传过来的数据id" + id);
            CraftsmanQO queryForPrimary = CraftsmanJTO.createQueryForPrimary(id);
            log.info("shop传过来的数据queryForPrimary" + queryForPrimary.toString());
            return returnUtil.getSuccess(BaseDTOJTO.toJson(manageInfoDomain.getCraftsmanShopList(queryForPrimary)));
        } catch (Throwable t) {
            log.info("shop传过来的数据失败" + t.toString());
            return dealException(t, returnUtil);
        }
    }


    /**
     * 根据店铺id 查询签约过店铺的发型师   也会存在 如果是 飞虎队 也存在签约的情况下(是数据库绑定)
     *
     * @param shopIds:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/29 17:38
     */
    @PostMapping("/shopIds")
    public String getShopCraftsman(@RequestBody List<Long> shopIds) {
        returnUtil = new ReturnUtil();
        try {
            log.info("shopIds传过来的数据集合" + shopIds.size());
            return returnUtil.getSuccess(BaseDTOJTO.toJson(manageInfoDomain.getShopIdsByServiceCraftsman(shopIds)));
        } catch (Throwable t) {
            log.info("shop传过来的数据失败" + t.toString());
            return dealException(t, returnUtil);
        }
    }


    /**
     * 外部接口，
     *
     * @return :
     * @author : gaozifeng
     * @date : 2019/5/15 10:43
     */
    @GetMapping("/manager/{shopid}")
    public ResponseUtils<CraftsmanShopDTO> getCraftsmanManagerName(@PathVariable String shopid) {
        try {
            CraftsmanShopQO queryForShopId = CraftsmanShopJTO.createQueryForShopId(shopid);
            CraftsmanShopDTO shopInfo = manageInfoDomain.getManagerName(queryForShopId);
            return ResponseUtils.getSuccess(shopInfo);
        } catch (Throwable t) {
            dealException(t);
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }


    /**
     * 根据店铺id 获取经理信息，总经理信息
     *
     * @param shopId:
     * @return : com.xingkeduo.pojo.utils.ResponseUtils<java.util.List<java.lang.Long>>
     * @author : gaozifeng
     * @date : 2019/7/12 18:16
     */
    @GetMapping("/getManagerList")
    public ResponseUtils<CraftsmanShopDTO> getManagerList(Long companyId, Long shopId) {
        try {
            return ResponseUtils.getSuccess(manageInfoDomain.getManagerList(companyId, shopId));
        } catch (Throwable t) {
            dealException(t);
            return ResponseUtils.error(t.getMessage(), "-1");
        }
    }

    /**
     * 查询可使用的店铺id
     *
     * @param :
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/31 17:55
     */
    @GetMapping("/canShop/list/{id}")
    public String canUseShop(@PathVariable String id) {
        returnUtil = new ReturnUtil();
        try {
            return returnUtil.getSuccess(BaseDTOJTO.toJson(manageInfoDomain.checkCanUseShopList(id)));
        } catch (Throwable t) {
            dealException(t);
            return dealException(t, returnUtil);
        }
    }
}
